home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 14 / CU Amiga Magazine's Super CD-ROM 14 (1997)(EMAP Images)(GB)(Track 1 of 3)[!][issue 1997-09].iso / CUCD / Programming / RKMLibsPrgs / intuition / screens / doublebuffer.c.bak < prev    next >
Text File  |  1992-09-03  |  8KB  |  250 lines

  1. ;/* doublebuffer.c - Show the use of a double-buffered screen.
  2. lc -b1 -cfist -v -y -j73 doublebuffer
  3. blink FROM LIB:c.o doublebuffer.o TO doublebuffer LIB LIB:lc.lib LIB:amiga.lib
  4. quit
  5. */
  6.  
  7. /*
  8. Copyright (c) 1992 Commodore-Amiga, Inc.
  9.  
  10. This example is provided in electronic form by Commodore-Amiga, Inc. for
  11. use with the "Amiga ROM Kernel Reference Manual: Libraries", 3rd Edition,
  12. published by Addison-Wesley (ISBN 0-201-56774-1).
  13.  
  14. The "Amiga ROM Kernel Reference Manual: Libraries" contains additional
  15. information on the correct usage of the techniques and operating system
  16. functions presented in these examples.  The source and executable code
  17. of these examples may only be distributed in free electronic form, via
  18. bulletin board or as part of a fully non-commercial and freely
  19. redistributable diskette.  Both the source and executable code (including
  20. comments) must be included, without modification, in any copy.  This
  21. example may not be published in printed form or distributed with any
  22. commercial product.  However, the programming techniques and support
  23. routines set forth in these examples may be used in the development
  24. of original executable software products for Commodore Amiga computers.
  25.  
  26. All other rights reserved.
  27.  
  28. This example is provided "as-is" and is subject to change; no
  29. warranties are made.  All use is at your own risk. No liability or
  30. responsibility is assumed.
  31. */
  32.  
  33.  
  34. #define INTUI_V36_NAMES_ONLY
  35.  
  36. #include <exec/types.h>
  37. #include <exec/memory.h>
  38. #include <intuition/intuition.h>
  39. #include <intuition/screens.h>
  40.  
  41. #include <clib/exec_protos.h>
  42. #include <clib/graphics_protos.h>
  43. #include <clib/intuition_protos.h>
  44.  
  45.  
  46. #ifdef LATTICE
  47. int CXBRK(void)    { return(0); }  /* Disable Lattice CTRL/C handling */
  48. int chkabort(void) { return(0); }  /* really */
  49. #endif
  50.  
  51.  
  52. /* characteristics of the screen */
  53. #define SCR_WIDTH  (320)
  54. #define SCR_HEIGHT (200)
  55. #define SCR_DEPTH    (2)
  56.  
  57.  
  58. /* Prototypes for our functions */
  59. VOID   runDBuff(struct Screen *, struct BitMap ** );
  60. struct BitMap **setupBitMaps( LONG, LONG, LONG );
  61. VOID   freeBitMaps(struct BitMap **,LONG, LONG, LONG );
  62. LONG   setupPlanes(struct BitMap *, LONG, LONG, LONG );
  63. VOID    freePlanes(struct BitMap *, LONG, LONG, LONG );
  64.  
  65.  
  66. struct Library *IntuitionBase = NULL;
  67. struct Library *GfxBase       = NULL;
  68.  
  69. /*
  70. ** Main routine.  Setup for using the double buffered screen.
  71. ** Clean up all resources when done or on any error.
  72. */
  73.  
  74. VOID main(int argc, char **argv)
  75. {
  76. struct BitMap **myBitMaps;
  77. struct Screen  *screen;
  78. struct NewScreen myNewScreen;
  79.  
  80. IntuitionBase = OpenLibrary("intuition.library", 33L);
  81. if ( IntuitionBase != NULL )
  82.     {
  83.     GfxBase = OpenLibrary("graphics.library", 33L);
  84.     if ( GfxBase != NULL )
  85.         {
  86.         myBitMaps = setupBitMaps(SCR_DEPTH, SCR_WIDTH, SCR_HEIGHT);
  87.         if ( myBitMaps != NULL )
  88.             {
  89.             /* Open a simple quiet screen that is using the first
  90.             ** of the two bitmaps.
  91.             */
  92.             myNewScreen.LeftEdge=0;
  93.             myNewScreen.TopEdge=0;
  94.             myNewScreen.Width=SCR_WIDTH;
  95.             myNewScreen.Height=SCR_HEIGHT;
  96.             myNewScreen.Depth=SCR_DEPTH;
  97.             myNewScreen.DetailPen=0;
  98.             myNewScreen.BlockPen=1;
  99.             myNewScreen.ViewModes=HIRES;
  100.             myNewScreen.Type=CUSTOMSCREEN | CUSTOMBITMAP | SCREENQUIET;
  101.             myNewScreen.Font=NULL;
  102.             myNewScreen.DefaultTitle=NULL;
  103.             myNewScreen.Gadgets=NULL;
  104.             myNewScreen.CustomBitMap=myBitMaps[0];
  105.  
  106.             screen = OpenScreen(&myNewScreen);
  107.             if (screen != NULL)
  108.                 {
  109.                 /* Indicate that the rastport is double buffered. */
  110.                 screen->RastPort.Flags = DBUFFER;
  111.  
  112.                 runDBuff(screen, myBitMaps);
  113.  
  114.                 CloseScreen(screen);
  115.                 }
  116.             freeBitMaps(myBitMaps, SCR_DEPTH, SCR_WIDTH, SCR_HEIGHT);
  117.             }
  118.         CloseLibrary(GfxBase);
  119.         }
  120.     CloseLibrary(IntuitionBase);
  121.     }
  122. }
  123.  
  124.  
  125.  
  126.  
  127.  
  128. /*
  129. ** setupBitMaps(): allocate the bit maps for a double buffered screen.
  130. */
  131. struct BitMap **setupBitMaps(LONG depth, LONG width, LONG height)
  132. {
  133. /* this must be static -- it cannot go away when the routine exits. */
  134. static struct BitMap *myBitMaps[2];
  135.  
  136. myBitMaps[0] = (struct BitMap *) AllocMem((LONG)sizeof(struct BitMap), MEMF_CLEAR);
  137. if (myBitMaps[0] != NULL)
  138.     {
  139.     myBitMaps[1] = (struct BitMap *)AllocMem((LONG)sizeof(struct BitMap), MEMF_CLEAR);
  140.     if (myBitMaps[1] != NULL)
  141.         {
  142.         InitBitMap(myBitMaps[0], depth, width, height);
  143.         InitBitMap(myBitMaps[1], depth, width, height);
  144.  
  145.         if (NULL != setupPlanes(myBitMaps[0], depth, width, height))
  146.             {
  147.             if (NULL != setupPlanes(myBitMaps[1], depth, width, height))
  148.                 return(myBitMaps);
  149.  
  150.             freePlanes(myBitMaps[0], depth, width, height);
  151.             }
  152.         FreeMem(myBitMaps[1], (LONG)sizeof(struct BitMap));
  153.         }
  154.     FreeMem(myBitMaps[0], (LONG)sizeof(struct BitMap));
  155.     }
  156. return(NULL);
  157. }
  158.  
  159. /*
  160. ** runDBuff(): loop through a number of iterations of drawing into
  161. ** alternate frames of the double-buffered screen.  Note that the
  162. ** object is drawn in color 1.
  163. */
  164. VOID runDBuff(struct Screen *screen, struct BitMap **myBitMaps)
  165. {
  166. WORD ktr, xpos, ypos;
  167. WORD toggleFrame;
  168.  
  169. toggleFrame = 0;
  170. SetAPen(&(screen->RastPort), 1);
  171.  
  172. for (ktr = 1; ktr < 200; ktr++)
  173.     {
  174.     /* Calculate a position to place the object, these
  175.     ** calculations insure the object will stay on the screen
  176.     ** given the range of ktr and the size of the object.
  177.     */
  178.     xpos = ktr;
  179.     if ((ktr % 100) >= 50)
  180.         ypos = 50 - (ktr % 50);
  181.     else
  182.         ypos = ktr % 50;
  183.  
  184.     /* switch the bitmap so that we are drawing into the correct place */
  185.     screen->RastPort.BitMap          = myBitMaps[toggleFrame];
  186.     screen->ViewPort.RasInfo->BitMap = myBitMaps[toggleFrame];
  187.  
  188.     /* Draw the objects.
  189.     ** Here we clear the old frame and draw a simple filled rectangle.
  190.     */
  191.     SetRast(&(screen->RastPort), 0);
  192.     RectFill(&(screen->RastPort), xpos, ypos, xpos+100, ypos+100);
  193.  
  194.     /* update the physical display to match the newly drawn bitmap. */
  195.     MakeScreen(screen); /* Tell intuition to do its stuff.          */
  196.     RethinkDisplay();   /* Intuition compatible MrgCop & LoadView   */
  197.                         /*               it also does a WaitTOF().  */
  198.  
  199.     /* switch the frame number for next time through */
  200.     toggleFrame ^= 1;
  201.     }
  202. }
  203.  
  204. /*
  205. ** freeBitMaps(): free up the memory allocated by setupBitMaps().
  206. */
  207. VOID freeBitMaps(struct BitMap **myBitMaps, LONG depth, LONG width, LONG height)
  208. {
  209. freePlanes(myBitMaps[0], depth, width, height);
  210. freePlanes(myBitMaps[1], depth, width, height);
  211.  
  212. FreeMem(myBitMaps[0], (LONG)sizeof(struct BitMap));
  213. FreeMem(myBitMaps[1], (LONG)sizeof(struct BitMap));
  214. }
  215.  
  216. /*
  217. ** setupPlanes(): allocate the bit planes for a screen bit map.
  218. */
  219. LONG setupPlanes(struct BitMap *bitMap, LONG depth, LONG width, LONG height)
  220. {
  221. SHORT plane_num ;
  222.  
  223. for (plane_num = 0; plane_num < depth; plane_num++)
  224.     {
  225.     bitMap->Planes[plane_num] = (PLANEPTR)AllocRaster(width, height);
  226.     if (bitMap->Planes[plane_num] != NULL )
  227.         BltClear(bitMap->Planes[plane_num], (width / 8) * height, 1);
  228.     else
  229.         {
  230.         freePlanes(bitMap, depth, width, height);
  231.         return(NULL);
  232.         }
  233.     }
  234. return(TRUE);
  235. }
  236.  
  237. /*
  238. ** freePlanes(): free up the memory allocated by setupPlanes().
  239. */
  240. VOID freePlanes(struct BitMap *bitMap, LONG depth, LONG width, LONG height)
  241. {
  242. SHORT plane_num ;
  243.  
  244. for (plane_num = 0; plane_num < depth; plane_num++)
  245.     {
  246.     if (bitMap->Planes[plane_num] != NULL)
  247.         FreeRaster(bitMap->Planes[plane_num], width, height);
  248.     }
  249. }
  250.